package top.eggcode.common.test;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.UUID;

/**
 * Title: 生成随机数据
 * Description: TODO
 * Date: 2021/4/13 13:32
 *
 * @author JiaQi Ding
 * @version 1.0
 */
public final class RandomDataFactory {

    /**
     * 数字库
     */
    public static final String NUMBER_STORE = "0123456789";
    /**
     * 字符库
     */
    private static final String CHAR_STORE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    /**
     * 字母库
     */
    private static final String LETTER_STORE = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 生成随机数
     *
     * @param min 最小值
     * @param max 最大值
     * @return 整数
     */
    public static int number(int min, int max) {
        Random random = new Random();
        return (random.nextInt(max) % (max - min + 1) + min);
    }

    /**
     * 生成随机整数，包括边界值
     *
     * @param max 边界值
     * @return 整数
     */
    public static int number(int max) {
        return new Random().nextInt(max + 1);
    }

    public static float nextFloat() {
        DecimalFormat df = new DecimalFormat("#.000000");

        return Float.parseFloat(
                df.format(new Random().nextFloat())
        );
    }

    public static double nextDouble() {
        DecimalFormat df = new DecimalFormat("#.000000");
        int number = number(10000);

        return Double.parseDouble(
                df.format(new Random().nextDouble() * number)
        );
    }


    /**
     * 固定长度的数字
     *
     * @param length 长度
     * @return 整数
     */
    public static String numberLength(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(NUMBER_STORE.charAt(random.nextInt(NUMBER_STORE.length())));
        }
        return sb.toString();
    }

    /**
     * 随机邮箱
     *
     * @return 字符串
     */
    public static String email() {
        int type = number(1, 5);
        String prefix = string();
        String suffix = "";
        switch (type) {
            case 1:
                suffix = "@qq.com";
                break;
            case 2:
                suffix = "@163.com";
                break;
            case 3:
                suffix = "@sina.com";
                break;
            case 4:
                suffix = "@126.com";
                break;
            case 5:
                suffix = "@gmail.com";
                break;
            case 6:
                suffix = "@msn.com";
                break;
            case 7:
                suffix = "@live.com";
                break;
            case 8:
                suffix = "@hotmail.com";
                break;
            default:
                suffix = "@email.com";
        }

        return prefix + suffix;
    }

    /**
     * 随机文本
     *
     * @return 字符串
     */
    public static String string() {
        return stringLength(number(1, 20));
    }

    /**
     * 随机字符串(只包含大小写字母、数字)
     *
     * @param length 长度
     * @return 字符串
     */
    public static String stringLength(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(CHAR_STORE.charAt(random.nextInt(CHAR_STORE.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯字母字符串(只包含大小写字母)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String stringLetter(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(CHAR_STORE.charAt(random.nextInt(LETTER_STORE.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String stringLower(int length) {
        return stringLetter(length).toLowerCase();
    }

    /**
     * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String stringUpper(int length) {
        return stringLetter(length).toUpperCase();
    }

    /**
     * 生成一个定长的纯0字符串
     *
     * @param length 字符串长度
     * @return 纯0字符串
     */
    public static String stringZero(int length) {
        StringBuilder str = new StringBuilder();
        for (int i = 1; i <= length; i++) {
            str.append("0");
        }
        return str.toString();
    }

    /**
     * 根据数字生成一个定长的字符串，长度不够前面补0
     *
     * @param num       数字
     * @param fixdlenth 字符串长度
     * @return 定长的字符串
     */
    public static String numberStringLength(long num, int fixdlenth) {
        StringBuilder sb = new StringBuilder();
        String strNum = String.valueOf(num);
        if (fixdlenth - strNum.length() >= 0) {
            sb.append(stringZero(fixdlenth - strNum.length()));
        } else {
            throw new RuntimeException("将数字" +
                    num + "转化为长度为" + fixdlenth + "的字符串发生异常！");
        }
        sb.append(strNum);
        return sb.toString();
    }


    /**
     * 从指定的数组中随机数组中的某个元素
     */
    public static <T> T item(T[] param) {
        int index = number(0, param.length);
        return param[index];
    }

    /**
     * 返回一个UUID
     *
     * @return 小写的UUID
     */
    public static String uuidLower() {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        return s.substring(0, 8) + s.substring(9, 13) +
                s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
    }

    /**
     * 返回一个UUID
     *
     * @return 大写的UUID
     */
    public static String uuidUpper() {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        String temp = s.substring(0, 8) + s.substring(9, 13) +
                s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
        return temp.toUpperCase();
    }


}
